//
//  NetWorking.m
//  jiajiao
//
//  Created by apple on 16/3/11.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "NetWorking.h"

@implementation NetWorking

+(void)Post:(NSString *)url
     params:(NSDictionary *)dic
    success:(void(^)(NSDictionary * response))success
    failuer:(void(^)(NSError *errog))failure
{
    NSString *allurl = [NSString stringWithFormat:@"%@%@",BaseUrl,url];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/xml",@"text/plain",nil];
    
    [manager POST:allurl parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        
        
        NSLog(@"%@",resultDic);
        
        //因为infor是固定的所以取出来转模型
//        NSDictionary *dic = resultDic[@"infor"][@"initPO"];
        success(resultDic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    
        NSLog(@"%@",error);
    
    }];
}

+(void)NoramlGet:(NSString *)url params:(NSDictionary *)dic success:(void (^)(NSDictionary *respose ,NSString*msg,NSString *succe ))success failuer:(void (^)( NSError *))failuer
{
    AppDelegate *deleget = Kapp;
    
    NSString *allurl;
//    if (deleget.ipmodel.sys_web_service) {
//        allurl= [NSString stringWithFormat:@"%@/%@",deleget.ipmodel.sys_web_service,url];
//    }else
//    {
//        NSLog(@"第一次请求未拿到地址");
//        
//        return;
//    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/xml",nil];
    [manager GET:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dic = [resultDic objectForKey:@"infor"];
        NSString *ste = [resultDic objectForKey:@"msg"];
        NSString *succ = [resultDic objectForKey:@"success"];

        success(dic,ste,succ);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failuer(error);
    }];

}

+(void)NormalUpdata:(NSString *)url
       params:(NSDictionary *)dic
        image:(UIImage *)image
      success:(void(^)(NSURLSessionDataTask *task,id responseObject))success
      failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure
{
    NSData *imageData = UIImageJPEGRepresentation(image, 0.03);
    
    AppDelegate *deleget = Kapp;
    
    NSString *allurl;
//    if (deleget.ipmodel.sys_web_service) {
//        allurl= [NSString stringWithFormat:@"%@/%@",deleget.ipmodel.sys_web_service,url];
//    }else
//    {
//        NSLog(@"第一次请求未拿到地址");
//        
//        return;
//    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/xml", @"text/plain",nil];
    NSLog(@"%@",dic);
    
    [manager POST:allurl parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
        [formData appendPartWithFileData:imageData name:@"myfile_" fileName:fileName mimeType:@"image/png"];
        
    } progress:nil success:success failure:failure];

}

+(void)NormalPost:(NSString *)url
           params:(NSDictionary *)dic
          success:(void(^)(NSDictionary * response,NSString *msg ,NSString *succe))success
          failuer:(void(^)(NSError *errog))failure
{
    AppDelegate *deleget = Kapp;
    NSString *allurl;
//    if (deleget.ipmodel.sys_web_service) {
//        allurl= [NSString stringWithFormat:@"%@/%@",deleget.ipmodel.sys_web_service,url];
//    }else
//    {
//        NSLog(@"第一次请求未拿到地址");
//        
//        return;
//    }
    NSLog(@"%@",allurl);

    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/xml",@"text/plain",nil];
    
    [manager POST:allurl parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        
        //因为infor是固定的所以取出来转模型
        NSDictionary *dic = [resultDic objectForKey:@"infor"];
        NSString *ste =[resultDic objectForKey:@"msg"];
        NSString *succ =[resultDic objectForKey:@"success"];
        success(dic,ste,succ);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];

}

//验证银行卡类型
+(void)checkCardPost:(NSString *)url
              params:(NSDictionary *)dic
             success:(void(^)(NSDictionary * response,NSString *msg ,NSString *succe))success
             failuer:(void(^)(NSError *errog))failure;
{
        
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/xml",@"text/plain",nil];
    
    
       [manager POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        
        //因为infor是固定的所以取出来转模型
        NSLog(@"%@",resultDic);
//        NSDictionary *dic = [resultDic objectForKey:@"infor"];
        NSString *ste =[resultDic objectForKey:@"msg"];
        NSString *succ =[resultDic objectForKey:@"success"];
        success(resultDic,ste,succ);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];



}


@end
